{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/embeddings/gemini.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Google Gemini Embeddings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index 'google-generativeai>=0.3.0' matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "GOOGLE_API_KEY = \"\"  # add your GOOGLE API key here\n",
    "os.environ[\"GOOGLE_API_KEY\"] = GOOGLE_API_KEY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# imports\n",
    "from llama_index.embeddings import GeminiEmbedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/haotianzhang/llama_index/venv/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "# get API key and create embeddings\n",
    "\n",
    "model_name = \"models/embedding-001\"\n",
    "\n",
    "embed_model = GeminiEmbedding(\n",
    "    model_name=model_name, api_key=GOOGLE_API_KEY, title=\"this is a document\"\n",
    ")\n",
    "\n",
    "embeddings = embed_model.get_text_embedding(\"Google Gemini Embeddings.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dimension of embeddings: 768\n"
     ]
    }
   ],
   "source": [
    "print(f\"Dimension of embeddings: {len(embeddings)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.028174246, -0.0290093, -0.013280814, 0.008629, 0.025442218]"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embeddings[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.028174246, -0.0290093, -0.013280814, 0.008629, 0.025442218]"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embeddings = embed_model.get_query_embedding(\"Google Gemini Embeddings.\")\n",
    "embeddings[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embeddings = embed_model.get_text_embedding(\n",
    "    [\"Google Gemini Embeddings.\", \"Google is awesome.\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dimension of embeddings: 2\n",
      "[0.028174246, -0.0290093, -0.013280814, 0.008629, 0.025442218]\n",
      "[0.009427786, -0.009968997, -0.03341217, -0.025396815, 0.03210592]\n"
     ]
    }
   ],
   "source": [
    "print(f\"Dimension of embeddings: {len(embeddings)}\")\n",
    "print(embeddings[0][:5])\n",
    "print(embeddings[1][:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.028174246, -0.0290093, -0.013280814, 0.008629, 0.025442218]\n"
     ]
    }
   ],
   "source": [
    "embedding = await embed_model.aget_text_embedding(\"Google Gemini Embeddings.\")\n",
    "print(embedding[:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.028174246, -0.0290093, -0.013280814, 0.008629, 0.025442218]\n",
      "[0.009427786, -0.009968997, -0.03341217, -0.025396815, 0.03210592]\n",
      "[0.013159992, -0.021570021, -0.060150445, -0.042500723, 0.041159637]\n"
     ]
    }
   ],
   "source": [
    "embeddings = await embed_model.aget_text_embedding_batch(\n",
    "    [\n",
    "        \"Google Gemini Embeddings.\",\n",
    "        \"Google is awesome.\",\n",
    "        \"Llamaindex is awesome.\",\n",
    "    ]\n",
    ")\n",
    "print(embeddings[0][:5])\n",
    "print(embeddings[1][:5])\n",
    "print(embeddings[2][:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.028174246, -0.0290093, -0.013280814, 0.008629, 0.025442218]\n"
     ]
    }
   ],
   "source": [
    "embedding = await embed_model.aget_query_embedding(\"Google Gemini Embeddings.\")\n",
    "print(embedding[:5])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  },
  "vscode": {
   "interpreter": {
    "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
